﻿using ShenNius.Blog.Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;
using System;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace ShenNius.Blog
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddHttpContextAccessor();
            //解决中文被编码
            services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            services.Configure<DomainConfig>(Configuration.GetSection("FrontDomain"));
            services.Configure<SiteConfig>(Configuration.GetSection("SiteConfig"));
            services.AddScoped(typeof(HttpHelper));
            services.AddHttpClient();
            services.AddResponseCompression();
            services.AddRouting(options => options.LowercaseUrls = true);
            
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseStatusCodePagesWithReExecute("/404.html");
               // app.UseExceptionHandler("/Home/Error");
            }
            app.UseResponseCompression();
            //静态文件缓存(css,js)
            app.UseStaticFiles(
                new StaticFileOptions
                {
                    OnPrepareResponse = ctx =>
                    {
                        const int durationInSeconds = 60 * 60 * 24;
                        ctx.Context.Response.Headers[HeaderNames.CacheControl] =
                            "public,max-age=" + durationInSeconds;
                    }
                });
            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            var options = new RewriteOptions();
            options.AddRewrite(@"(\w+)/(\w+)/page_(\d+).html", "/Home/NewsList?parentColumnUrl=$1&childColumnUrl=$2&page=$3", true);
            options.AddRewrite(@"(\w+)/page_(\d+).html", "/Home/NewsList?parentColumnUrl=$1&page=$2", true);
            options.AddRewrite(@"(\w+)/(\w+)/(\d+).html", "/Home/Detail?parentColumnUrl=$1&childColumnUrl=$2&id=$3", true);
            options.AddRewrite(@"(\w+)/(\d+).html", "/Home/Detail?parentColumnUrl=$1&id=$2", true);
            options.Add(WebHelper.RedirectXmlFileRequests);
            //将非 www 重新定向到 www：
            // options.AddRedirectToWwwPermanent();
            //options.AddRedirectToHttps(statusCode: 301, sslPort: 9527);
            app.UseRewriter(options);
                       
            //app.UseStatusCodePages();
            app.UseStatusCodePagesWithReExecute("/404.html");
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("parentColumnUrlChild", "{parentColumnUrl}/{childColumnUrl}/",
            new { controller = "Home", action = "NewsList", page = 1 }
            );
                endpoints.MapControllerRoute("parentColumnUrl", "{parentColumnUrl}/",
                 new { controller = "Home", action = "NewsList", page = 1 }
                 );
                
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });           
        }
    }
}
